package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    SetmealDishService sdService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.saveWithSetMealDish(setmealDto);
        return R.success("添加成功！");
    }

    /**
     * 套餐分页
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //先查询接班套餐信息，再把类别名封装上
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        setmealService.page(setmealPage,queryWrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        List<Setmeal> setmeals = setmealPage.getRecords();
        List<SetmealDto> setmealDtos = setmeals.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            String categoryName = categoryService.getById(item.getCategoryId()).getName();

            if(categoryName!=null){
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtos);
        return R.success(setmealDtoPage);
    }

//    请求网址: http://localhost:8080/setmeal/1631285039494983681
//    请求方法: GET

    /**
     * 根据Id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto=setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> updae(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithSetMealDish(setmealDto);
        return R.success("修改成功！");
    }

    /**
     * 修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> sale(@PathVariable("status") int status,Long []ids){
        List<Setmeal> list = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            list.add(setmeal);
        }
        setmealService.updateBatchById(list);
        return R.success("状态修改成功！");
    }

    /**
     * 删除套餐
     * 起售状态不能删除
     * 删除套餐对应的菜品列表
     */
    @DeleteMapping
    public R<String> delete(String []ids){
        int t = 0;
        for (String id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            if(setmeal.getStatus()!=1){
                setmealService.removeById(id);
                LambdaQueryWrapper <SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SetmealDish::getSetmealId,id);
                sdService.remove(queryWrapper);
            }else {
                t++;
            }
        }
        if(t>0&&t== ids.length){
            return R.error("所选套餐为起售状态，不能删除！");
        }else{
            return R.success("删除成功！");
        }
    }

    //请求网址: http://localhost:8080/setmeal/list?categoryId=1631280984743772161&status=1
    //请求方法: GET

    /**
     * 根据套餐类别 获取套餐列表
     * @param setmeal
     * @return
     */
    @GetMapping("list")
    public R<List> getSetMealDishes(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        //判断是否起售
        return R.success(list);
    }

    //GET /setmeal/dish/1631530823733764097

    @GetMapping("/dish/{setmealId}")
    public R<List> dish(@PathVariable("setmealId") Long setmealId){
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = sdService.list(queryWrapper);
        return R.success(list);
    }
}
